var db = require('../utils/db');

var userInfo = {};
var userAccount = {};

//初始化用户信息
exports.initUserInfo = function () {
    db.get_all_user_info(function (rows) {
        if (rows != null) {
            for (var i = 0; i < rows.length; ++i) {
                if (rows[i] != null) {
                    var userinfo = {
                        userid: rows[i].userid,
                        account: rows[i].account,
                        name: rows[i].name,
                        sex: rows[i].sex,
                        headimg: rows[i].headimg,
                        lv: rows[i].lv,
                        exp: rows[i].exp,
                        coins: rows[i].coins,
                        gems: rows[i].gems,
                        roomid: rows[i].roomid,
                        online: rows[i].online,
                        create_time: rows[i].create_time,
                        ip: rows[i].regIp
                    };
                    userInfo[rows[i].userid] = userinfo;
                    userAccount[rows[i].account] = rows[i].userid;
                }
            }
        }
        //console.log("user info = ", userInfo, userAccount,Object.keys(userInfo).length);
    });
}

//根据账号获取id
exports.getUserIdByAccount = function (account) {
    if (userAccount[account] != null) {
        return userAccount[account];
    }
    else {
        return 0;
    }
}

//根据ID返回玩家信息数据
exports.getUserInfoByAccount = function (account) {
    if (userAccount[account] != null) {
        var userid = userAccount[account];
        return userInfo[userid];
    }
    else {
        return null;
    }
}

//保存新进入玩家
exports.storeNewUser = function (userinfo) {
    if (userinfo == null) {
        console.log("err: store new user failed for user info in memory is null!");
        return;
    }
    if (userInfo[userinfo.userid] == null){
        userInfo[userinfo.userid] = userinfo;
    }
    if (userAccount[userinfo.account] == null){
        userAccount[userinfo.account] = userinfo.userid;
    }
}

//设置玩家对应房间表示
exports.setRoomIdOfUser = function (roomId, userId) {
    if (userInfo[userId] != null) {
        userInfo[userId].roomid = roomId;
    }
    else {
        console.log("err: setRoomIdOfUser failed! ")
    }
}

//定时更新玩家信息
exports.update = function () {
    db.get_all_user_info(function (rows) {
        if (rows != null) {
            //console.log("rows length ",rows.length);
            for (var i = 0; i < rows.length; ++i) {
                if (rows[i] != null) {
                    var userid = rows[i].userid;
                    if (userInfo[userid] === undefined
                        || rows[i].roomid != userInfo[userid].roomid
                        || rows[i].name != userInfo[userid].name
                        || rows[i].sex != userInfo[userid].sex
                        || rows[i].headimg != userInfo[userid].headimg
                        || rows[i].coins != userInfo[userid].coins
                        || rows[i].gems != userInfo[userid].gems) {

                        console.log("user change 1 ",userInfo[userid], userAccount[rows[i].account]);
                        if (userInfo[userid] === undefined){
                            userInfo[userid] = {};
                            if (rows[i].account != null){
                                console.log("change userid account ", userid, rows[i].account);
                                userAccount[rows[i].account] = userid;
                            }
                        }

                        userInfo[userid].userid = rows[i].userid;
                        userInfo[userid].account = rows[i].account;
                        userInfo[userid].name = rows[i].name;
                        userInfo[userid].sex = rows[i].sex;
                        userInfo[userid].headimg = rows[i].headimg;
                        userInfo[userid].coins = rows[i].coins;
                        userInfo[userid].roomid = rows[i].roomid;
                        userInfo[userid].gems = rows[i].gems;
                        userInfo[userid].online = rows[i].online;
                        userInfo[userid].isConfigPai = rows[i].isConfigPai; 
                        userInfo[userid].regIp = rows[i].regIp; 
                        userInfo[userid].isRobot = rows[i].isRobot; 
                        
                        console.log("user change = ", userid, rows[i]);
                    }
                }
            }
        }
    });
}